package cn.hncj.lyc.controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hncj.lyc.bean.ConsigneeManagement;
import cn.hncj.lyc.bean.GoodsInfo;
import cn.hncj.lyc.bean.OrderDetail;
import cn.hncj.lyc.bean.OrderManagement;
import cn.hncj.lyc.bean.UserInfo;
import cn.hncj.lyc.service.ConsigneeManagementService;
import cn.hncj.lyc.service.OrderManagementService;
import cn.hncj.lyc.service.OrderStateService;

/**
 * 登陆之后的关键操作（结算、提交订单）
 * 
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/admin/")
public class AdminController {

	@Autowired
	private ConsigneeManagementService cmService;

	@Autowired
	private OrderManagementService omService;
	@Autowired
	OrderStateService orderStateService;
	
	/*
	 * 
	 * 前端订单管理
	 * 
	 * @RequestMapping("/") public String FrontOrderController {
	 * 
	 * }
	 */

	@RequestMapping("ordercheck")
	public String orderCheck(String[] keys, Integer[] number, HttpSession session, Model model) {
		Map<String, GoodsInfo> cart = (Map<String, GoodsInfo>) session.getAttribute("cart");
		double sum = 0;// 总价
		//System.out.println("111111111");
		List<OrderDetail> list = new ArrayList<>();
		for (int i = 0; i < number.length; i++) {
			OrderDetail od = new OrderDetail();// 订单项
			GoodsInfo goodsInfo = cart.get(keys[i]);
			od.setGoodsId(goodsInfo.getGoodsId());
			od.setGoodsName(goodsInfo.getGoodsName());
			od.setOrderPrice(goodsInfo.getGoodsPrice());
			od.setGoodsUrl(goodsInfo.getGoodsUrl());
			od.setOrdeNumber(number[i]);
			od.setEveryTotal(number[i] * Double.parseDouble(goodsInfo.getGoodsPrice()));
			list.add(od);
			sum += number[i] * Double.parseDouble(goodsInfo.getGoodsPrice());// 总价
		}
		int num = 0;// 数量
		for (int i : number) {
			num += i;
		}
		model.addAttribute("list", list);
		model.addAttribute("sum", sum);
		model.addAttribute("num", num);

		ConsigneeManagement cm = new ConsigneeManagement();
		UserInfo userInfo = (UserInfo) session.getAttribute("loginuser");
		cm.setUserId(userInfo.getUserId());

		List<ConsigneeManagement> cmList = cmService.getCmList(cm);
		model.addAttribute("cmlist", cmList);

		return "front/order_check";
	}

	@RequestMapping("orderconfirm")
	public String orderConfirm(OrderManagement om, String[] goodsInfos, HttpSession session, Model model) {
		UserInfo userInfo = (UserInfo) session.getAttribute("loginuser");
		om.setUserId(userInfo.getUserId());
		om.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

		// 构建订单详情

		List<OrderDetail> list = new ArrayList<>();
		System.out.println(goodsInfos[1]);
		if (goodsInfos[1].length() > 1) {
			for (String str : goodsInfos) {
				String[] goodsInfo = str.split(",");
				OrderDetail od = new OrderDetail();
				//od.setGoodsId(Integer.parseInt(goodsInfo[0]));
				od.setGoodsId(goodsInfo[0]);
				od.setOrdeNumber(Integer.parseInt(goodsInfo[1]));
				od.setOrderPrice(goodsInfo[2]);
				od.setEveryTotal(Double.parseDouble(goodsInfo[1]) * Double.parseDouble(goodsInfo[2]));
				list.add(od);

			}
		} else {
			for (int i = 2; i < goodsInfos.length; i++) {
				// String[] goodsInfo = i.split(",");
				OrderDetail od = new OrderDetail();
				od.setGoodsId(goodsInfos[0]);
				od.setOrdeNumber(Integer.parseInt(goodsInfos[1]));
				od.setOrderPrice(goodsInfos[2]);
				od.setEveryTotal( Integer.parseInt(goodsInfos[1]) *  Double.parseDouble(goodsInfos[2]));
				list.add(od);

			}
			
		}

		try {
			omService.add(om, list);
			//OrderManagement order=
			model.addAttribute("order", om);
			System.out.println(om);
			//orderStateService.updateOrderState(om);
			session.removeAttribute("cart");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			model.addAttribute("info", "没有选择收货人");
			return "front/orderno";

		}

		return "pay/selectpay";

	}
	@RequestMapping("orderpay")
	public String orderpay() {
		return "front/pay";
	}
}
